package com.hieu.sms.web.mb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.SelectEvent;

import com.hieu.sms.model.Clazz;
import com.hieu.sms.model.Department;
import com.hieu.sms.model.Major;
import com.hieu.sms.model.Teacher;
import com.hieu.sms.model.enumtype.AcademicTitles;
import com.hieu.sms.model.enumtype.ContractType;
import com.hieu.sms.model.enumtype.Degree;
import com.hieu.sms.model.enumtype.TeacherType;
import com.hieu.sms.model.filter.TeacherFilter;
import com.hieu.sms.services.InformationServices;

@ManagedBean(name = "mbTeacher")
@ViewScoped
public class MbTeacher extends
		AbstractManagedBeanSimple<Teacher, TeacherFilter> {

	private Map<String, String> depMap;

	private Map<String, String> majorMap;
	
	private Map<String, String> clazzMap;

	private List<Teacher> teachers;

	@ManagedProperty("#{informationServices}")
	private InformationServices informationServices;

	public void setInformationServices(InformationServices informationServices) {
		this.informationServices = informationServices;
	}

	@PostConstruct
	public void init() {
		try {
			List<Department> departments = informationServices.list();
			depMap = new HashMap<String, String>(departments.size());
			for (Department department : departments) {
				depMap.put(department.getName(), department.getDepId());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void searchMajor(String depId) throws Exception {
		List<Major> majors = informationServices.findMajor(depId);
		majorMap = new HashMap<String, String>(majors.size());

		for (Major major : majors) {
			majorMap.put(major.getName(), major.getMajorId());
		}
	}
	
	private void searchClazz(String depId) throws Exception {
		List<Clazz> clazzs = informationServices.findClazz(depId);
		clazzMap = new HashMap<String, String>(clazzs.size());

		for (Clazz clazz : clazzs) {
			majorMap.put(clazz.getName(), clazz.getClazzId());
		}
	}

	private String getTeacherId(String depId) {
		String teacherId = "GV_" + depId + "_";
		int size = this.list.size() + 1;
		teacherId += size;
		return teacherId;
	}

	@Override
	protected List<Teacher> searchListCallback(TeacherFilter filter)
			throws Exception {
		searchMajor(filter.getDepId());
		searchClazz(filter.getDepId());
		return informationServices.findTeacher(filter);
	}

	@Override
	protected String deleteCallback(Teacher item) throws Exception {

		return null;
	}

	@Override
	protected String addCallback(Teacher item) throws Exception {
		informationServices.add(item);
		return null;
	}

	@Override
	protected String modifyCallback(Teacher item) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String deleteCallback(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getId(Teacher item) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, String> getDepMap() {
		return depMap;
	}

	public Map<String, String> getMajorMap() {
		return majorMap;
	}
	
	public Map<String, String> getClazzMap() {
		return clazzMap;
	}

	public List<Teacher> getTeachers() {
		return teachers;
	}
	
	public Degree[] getDegree(){
		return Degree.values();
	}
	
	public AcademicTitles[] getAcademicTitles(){
		return AcademicTitles.values();
	}
	
	public TeacherType[] getTeacherType(){
		return TeacherType.values();
	}
	public ContractType[] getContractType(){
		return ContractType.values();
	}
	public void handleDepartmentSelected() {

	}

	public void prepareAdd() {
		this.item = new Teacher();
		String teacherId = getTeacherId(filter.getDepId());
		this.item.setTeacherId(teacherId);
		this.item.setDepId(filter.getDepId());

	}

	public void prepareEdit() {
		
	}

	public void handleClose(CloseEvent event) {

	}
	
	public void onRowSelect(SelectEvent event){
		this.editMode = true;
		this.item = this.selected;
	}

}
